package com.chanjiedata.changbs.module.createcard;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.adapter.CreateSelectCardAdapter;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Mystock;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CreateSelectCardActivity extends BaseActivity {

    @BindView(R.id.back_tv)
    TextView backTv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.et_search)
    EditText etSearch;

    private CreateSelectCardAdapter mAdapter;
    private List<Mystock.showProductSNCode.List> datas = new ArrayList<>();
    private String goodsId = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_select_card);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        initRecyclerView();
        initListener();
        tvTitle.setText("制卡");
        initData();
    }

    private void initListener() {
        backTv.setOnClickListener(view -> finish());
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    goodsId = "1";
                } else {
                    goodsId = s.toString();
                }
                initData();
            }
        });

    }

    private void initData() {
        Mystock.showProductSNCode.Builder builder = Mystock.showProductSNCode.newBuilder();
        builder.setGoodsId(goodsId);
        NetworkManager.INSTANCE.post(Apis.get_sn_list, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                List<Mystock.showProductSNCode.List> lists = Mystock.showProductSNCode.parseFrom(bytes).getListList();
                mAdapter.replaceData(lists);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void initRecyclerView() {

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new CreateSelectCardAdapter(mContext, datas));
        mAdapter.bindToRecyclerView(recyclerView);
        mAdapter.setEmptyView(R.layout.recycle_empty);
        // 每个item的点击事件
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mContext, CreateCardActivity.class);
            intent.putExtra("data",getIntent().getStringExtra("data"));
            intent.putExtra("snCode",datas.get(position).getSnCode());
            startActivity(intent);
            finish();
        });
    }

}
